//
//  BaseDataSource.m
//  OrderSeat
//
//  Created by wangwei on 13-10-11.
//  Copyright (c) 2013年 wangwei. All rights reserved.
//

#import "BaseDataSource.h"
#import "ASINetworkQueue.h"

@implementation BaseDataSource

- (id)init
{
    if ( self = [super init] ) {
        [ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]];
        _requestArray = [[NSMutableArray alloc] initWithCapacity:2];
    }
    return self;
}

- (id)initWithVC:(id)Controller
{
    self = [self init];
    if (self) {
        self.viewContr = Controller;
    }
    return self;
}

- (id)initWithView:(id)view
{
    return [self init];
}

- (void)dealloc
{
    [self cancel];
}

- (void)cancel
{
    for (id request in _requestArray) {
        [request cancel];
        [request setDelegate:nil];
    }
    
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

- (void)get:(NSString*)aURL
{
    [self get:aURL tag:999];
}

- (void)get:(NSString*)aURL
        tag:(int)tag
{
    NSString *urlStr = [aURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:urlStr];
    
    CLog(@"url=%@",url);
    
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setTag:tag];
    [request setDelegate:self];
    [request startAsynchronous];
    
    [_requestArray addObject:request];
    
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

-(void)post:(NSString*)aURL
 dataString:(NSString*)dataString
{
    [self post:aURL dataString:dataString tag:9999];
}

-(void)post:(NSString*)aURL
 dataString:(NSString*)dataString
        tag:(int)tag
{
    [self post:aURL
          data:[dataString dataUsingEncoding:NSUTF8StringEncoding]
   extraParams:nil tag:tag];
}

- (void)post:(NSString*)aURL
        data:(NSData *)data
 extraParams:(NSDictionary *) params
{
    [self post:aURL data:data extraParams:params tag:99999];
}

-(void)post:(NSString*)aURL
     params:(NSDictionary *) params
{
    [self post:aURL params:params tag:99998];
}

-(void)post:(NSString*)aURL
     params:(NSDictionary *) params
        tag:(int)tag
{
    NSString *URL = (NSString*)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)aURL, (CFStringRef)@"%", NULL, kCFStringEncodingUTF8));
	NSURL *finalURL = [NSURL URLWithString:URL];
    
    CLog(@"url:%@",finalURL);
    
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:finalURL];
    
    for (NSString *key in params.allKeys) {
        CLog(@"key:%@",key);
        [request setPostValue:[params objectForKey:key] forKey:key];
    }
    
    [request setTag:tag];
    [request setDelegate:self];
    [request setStringEncoding:NSUTF8StringEncoding];
    [request startAsynchronous];
    
    [_requestArray addObject:request];
    
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

- (void)post:(NSString*)aURL
        data:(NSData *)data
 extraParams:(NSDictionary *) params
         tag:(int)tag
{
    NSString *URL = (NSString*)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)aURL, (CFStringRef)@"%", NULL, kCFStringEncodingUTF8));
	NSURL *finalURL = [NSURL URLWithString:URL];
    
    CLog(@"url:%@",finalURL);
    
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:finalURL];
    
    for (NSString *key in params.allKeys) {
        [request setPostValue:[params objectForKey:key] forKey:key];
    }
    
    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    [request setStringEncoding:enc];
    [request setTag:tag];
    [request setData:data withFileName:@"avatar.jpg" andContentType:@"image/jpeg" forKey:@"filename"];
    
    [request setDelegate:self];
    [request startAsynchronous];
    
    [_requestArray addObject:request];
    
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}


- (void)post:(NSString*)aURL
        file:(NSString *)file
 extraParams:(NSDictionary *)params
{
    [self post:aURL file:file extraParams:params tag:999999];
}

- (void)post:(NSString*)aURL
        file:(NSString *)file
 extraParams:(NSDictionary *)params
         tag:(int)tag
{
    NSString *URL = (NSString*)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)aURL, (CFStringRef)@"%", NULL, kCFStringEncodingUTF8));
	NSURL *finalURL = [NSURL URLWithString:URL];
    
    CLog(@"url:%@",finalURL);
    
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:finalURL];
    
    for (NSString *key in params.allKeys) {
        [request setPostValue:[params objectForKey:key] forKey:key];
    }
    
     NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    [request setStringEncoding:enc];
    [request setFile:file forKey:@"photo"];
    [request setTag:tag];
    [request setDelegate:self];
    [request startAsynchronous];
    
    [_requestArray addObject:request];
    
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

- (void)postEmptyFile:(NSString*)aURL
 extraParams:(NSDictionary *)params
         tag:(int)tag
{
    NSString *URL = (NSString*)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)aURL, (CFStringRef)@"%", NULL, kCFStringEncodingUTF8));
	NSURL *finalURL = [NSURL URLWithString:URL];
    
    CLog(@"url:%@",finalURL);
    
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:finalURL];
    
    for (NSString *key in params.allKeys) {
        [request setPostValue:[params objectForKey:key] forKey:key];
    }
    
    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    
    [request setStringEncoding:enc];
    [request setTag:tag];
    [request setDelegate:self];
    [request addRequestHeader:@"Content-Type" value:@"multipart/form-data"];
    [request startAsynchronous];
    
    [_requestArray addObject:request];
    
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}


//批量上传图片用的
- (void)post:(NSString*)aURL
  filesArray:(NSArray *)files
 extraParams:(NSDictionary *)params
{
    [self post:aURL filesArray:files extraParams:params tag:9999999];
}

- (void)post:(NSString*)aURL
   filesArray:(NSArray *)files
 extraParams:(NSDictionary *)params
         tag:(int)tag
{
    NSString *URL = (NSString*)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)aURL, (CFStringRef)@"%", NULL, kCFStringEncodingUTF8));
	NSURL *finalURL = [NSURL URLWithString:URL];
    
    CLog(@"url:%@",finalURL);
    
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:finalURL];
    
    for (NSString *key in params.allKeys) {
        [request setPostValue:[params objectForKey:key] forKey:key];
    }
    
    int i = 1;
    for (NSString *file in files) {
        [request setFile:file forKey:[NSString stringWithFormat:@"upload_%d", i]];
        i++;
    }
    
    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    
    [request setStringEncoding:enc];
    [request setTag:tag];
    [request setDelegate:self];
    [request setStringEncoding:NSUTF8StringEncoding];
    [request startAsynchronous];
    
    [_requestArray addObject:request];
    
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    CLog(@"request:%@",request.responseString);
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    [SVProgressHUD dismiss];
    [SVProgressHUD showErrorWithStatus:@"请求失败，请检查网络！" duration:1.2];
}

#pragma mark -
#pragma mark UIScrollViewDelegate Methods

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
	if (_viewContr.refreshHeaderView)
	{
        [_viewContr.refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
	if (_viewContr.refreshHeaderView)
	{
        [_viewContr.refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
    }
}

@end
